package com.fc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fc.entity.SysPermission;
import com.fc.mapper.SysPermissionMapper;
import com.fc.service.SysPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author zffc
 * @since 2023-07-18
 */
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements SysPermissionService {

    @Override
    public boolean getPermisisonByURL(String requestURI) {
        LambdaQueryWrapper<SysPermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysPermission::getUrl,requestURI);
        SysPermission sysPermission = getOne(queryWrapper,false);
        return sysPermission == null ? false: true;

    }

    @Override
    public List<String> getPermissionUrlsByUsername(String username) {
        return baseMapper.selectPermissionUrlByUsername(username);
    }

    @Override
    public List<SysPermission> getPermisisonList() {
        LambdaQueryWrapper<SysPermission> firstQuery = new LambdaQueryWrapper<SysPermission>();
        firstQuery.eq(SysPermission::getMenuType,0)
                .eq(SysPermission::getStatus,1)
                .orderByAsc(SysPermission::getGrade);
        List<SysPermission> list = list(firstQuery);
        list.forEach( e -> {
            LambdaQueryWrapper<SysPermission> secondQuery = new LambdaQueryWrapper<SysPermission>();
            secondQuery.eq(SysPermission::getParentId,e.getId())
                    .eq(SysPermission::getMenuType,1)
                    .eq(SysPermission::getStatus,1)
                    .orderByAsc(SysPermission::getGrade);
            e.setChild(list(secondQuery));
        });
        return list;
    }
}
